package com.example.md;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.inputmethodservice.InputMethodService;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import com.example.mmd.MobileClient;
import com.remote_control_mobile.remote_control.R;

public class LoginActivity extends Activity {
	
	Button loginBtn;
	EditText nameText;
	EditText passwordText;
	CheckBox rememberUserCheckbox;
	public static MobileClient myClient;
	static int show_picker;
	String userInfoFileName = "userinfo.bin";
	
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
	        setContentView(R.layout.activity_login);
	        nameText = (EditText)findViewById(R.id.editText1);
	        passwordText = (EditText)findViewById(R.id.editText2);
	        loginBtn = (Button)findViewById(R.id.button10);
	        rememberUserCheckbox = (CheckBox)findViewById(R.id.checkBox1);
	         
	 }
	 
		private void checkIfRemember(){
			File userInfoFile = new File(userInfoFileName);
			if(userInfoFile.exists()){
				rememberUserCheckbox.setChecked(true);
				 byte[] buffer = new byte[(int)userInfoFile.length()];
			        try {
			        	 FileInputStream fin = new FileInputStream(userInfoFile);
			            fin.read(buffer);
			            fin.close();
			        }catch(Exception e){
			        	System.err.println(e);
			        }
			        String s = new String(buffer);
			        String[] userInfo = s.split(" ");
			        nameText.setText(userInfo[0]);
			        passwordText.setText(userInfo[1]);
			        
			}
			else{
				rememberUserCheckbox.setChecked(false);
			}	
		}
		private static void processBuffer(byte[] buffer, int start, int end) {
	        System.out.write(buffer, start, end);
	    }
		private void saveUserInfo(String name,String pass){
			try{
				DataOutputStream os = new DataOutputStream(new FileOutputStream(userInfoFileName));
				String info = name + " " + pass;
				byte[] buffer = info.getBytes();
				os.write(buffer);
				 os.close();
			}catch(Exception e){
			}
		}
	 
	 
	 public void loginToLore(View view){
		 String username = nameText.getText().toString();
		 String password = passwordText.getText().toString();
		 if(rememberUserCheckbox.isChecked())
			{
				saveUserInfo(username,password);
				System.err.println("Not delete");
			}
			else{
				File userInfoFile = new File(userInfoFileName);
				if(userInfoFile.exists()){
					
					if(userInfoFile.delete()){

						System.err.println("delete succeed");
					}
					else{
						System.err.println("delete failed");
					}
					
				}
			}
		  try{
	        	myClient = new MobileClient(username+"&"+password,"lore.cs.purdue.edu");
	        }catch(Exception e){
	        }
		    Intent intent = new Intent(this,PPTActivity.class);
			startActivity(intent);
	 }
	 
	 
}
